//并行
#include <iostream>
#include <vector>
#include <numeric>
#include <future>
#include <chrono>
#include <iomanip>

long long partialSquareSum(int index) {
    long long sum = 0;
    for (size_t i = 0; i < 10000000; ++i) {
        sum += 2 * 2;
    }

    // 获取当前时间并格式化
    auto now = std::chrono::system_clock::now();
    auto now_ms = std::chrono::time_point_cast<std::chrono::microseconds>(now);  // 获取微秒级时间戳
    auto epoch = now_ms.time_since_epoch();
    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch);
    auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(epoch) - std::chrono::duration_cast<std::chrono::microseconds>(seconds);

    std::time_t now_c = std::chrono::system_clock::to_time_t(now);
    std::tm tm;
    localtime_s(&tm, &now_c);

    // 打印线程的结果和时间戳（精确到微秒）
   std::cout << "thread" << index << ": time: " << sum 
               << " time: " << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") 
               << "." << std::setw(6) << std::setfill('0') << microseconds.count() << std::endl;
    
    return sum;
}

int main() {
    // 定义并行任务的数量
    const int taskCount = 3;
    std::vector<std::future<long long>> futures;

    for (int i = 0; i < taskCount; ++i) {
        futures.emplace_back(std::async(std::launch::async,partialSquareSum,i + 1));
    }

    long long result = 0;
    for (auto& f : futures) {
        result += f.get();  // 获取每个任务的结果并累加
    }

    std::cout << "parallel: " << result << std::endl;

    return 0;
}
